package com.djsearch.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.djsearch.R;
import com.djsearch.bean.GroupBuyGoods;
import com.djsearch.bean.Movie;
import com.djsearch.data.ImageManager;
import com.djsearch.data.RequestImage;
import com.djsearch.util.AsyncImageLoader;
import com.djsearch.util.AsyncImageLoader.ImageCallback;

public class MovieAdapter extends BaseAdapter {

	private Context mContext;
	private List<Movie> movieList;
	private Bitmap mbitmap;
	public MovieAdapter(Context context, List<Movie> list) {
		mContext = context;
		movieList = list;
		Bitmap bitmap = ImageManager.drawableToBitmap(mContext.getResources().getDrawable(R.drawable.taogou),150.0f);
		mbitmap = (ImageManager.resizeImage(bitmap, 150.0f));
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return movieList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return movieList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public void refreshData(List<Movie> list) {
		movieList = list;
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ViewHolder holder;
		holder = new ViewHolder();
		if (convertView == null) {
			LayoutInflater layoutInflater = ((Activity) mContext)
					.getLayoutInflater();
			convertView = layoutInflater.inflate(R.layout.movie_item, parent,
					false);
			convertView.setTag(convertView);
		} else {
			convertView = (View) convertView.getTag();
		}
		holder.movie_icon = (ImageView) convertView
		.findViewById(R.id.movie_icon);
		
		RequestImage.getIntance(mbitmap, "Movie").download(movieList.get(position).getSmallImage(), 
				holder.movie_icon);
		
        holder.distance = (TextView) convertView.findViewById(R.id.juli);
        holder.address = (TextView) convertView.findViewById(R.id.address);

        holder.price = (TextView) convertView.findViewById(R.id.price);
        holder.shangquan = (TextView) convertView
		.findViewById(R.id.shangquang);
        holder.map = (TextView) convertView.findViewById(R.id.map);
        holder.phone = (TextView) convertView.findViewById(R.id.phone);
		holder.distance.setText(movieList.get(position).getDistance());
		holder.address.setText(movieList.get(position).getPlace());
		if (movieList.get(position).getPrice().equals("null")) {
			holder.price.setText("");
		} else {
			holder.price.setText(movieList.get(position).getPrice());
		}
		
		return convertView;
	}

	public static class ViewHolder {
		ImageView movie_icon;
		TextView cinema;
		TextView distance;
		TextView address;
		TextView price;
		TextView shangquan;
		TextView map;
		TextView phone;
	}
}
